package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.model.Domain;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.views.figures.DomainFigure;

public class DomainEditpart extends AbstractNodeEditPart {
	public DomainEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	protected void createEditPolicies() {
		super.createEditPolicies();

		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}

	private Domain getDomain() {
		return (Domain) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		// super.refreshVisuals();
		Rectangle constraint = new Rectangle(0, 0, -1, -1);
		MetaNode node = getNode();
		Point p = new Point(node.getLocationX(), node.getLocationY());
		constraint.setLocation(p);
		constraint.width = node.getWidth();
		constraint.height = node.getHeight();
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);

		Domain modelEle = (Domain) getNode().getModelElement();
		DomainFigure figure = (DomainFigure) getFigure();

	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.DOMAIN_NEGATIVE.equals(propertyName)) {
			DomainFigure f = (DomainFigure) this.getFigure();
			f.setAnti((Boolean) event.getNewValue());

		} else {
			super.handlePropertyChanged(event);
		}
	}

}
